var workspace;
var code;
workspace = Blockly.inject('blocklyDiv', {
	media: './media/',
	toolbox: document.getElementById('toolbox'),
	zoom: {
		controls: true,
		startScale: 1.2
	},
	trashcan: true
});

$(".blocklyScrollbarHandle").eq(0).css('width',10+'px');
$(".blocklyScrollbarHandle").eq(0).css('opacity','1');
$(".blocklyScrollbarHandle").eq(0).css('fill','gray');
//$(".blocklyScrollbarHandle").eq(1).css('height',10+'px');
//$(".blocklyScrollbarHandle").eq(1).css('opacity','1');
//$(".blocklyScrollbarHandle").eq(1).css('fill','gray');
//打开新建项目对话框
$("#creatprogram").on('click', function() {
	$("#creatpromodal").css('display', 'block');
	console.log(barpp);
	
});
//关闭新建项目对话框
$("#closebtn").on('click', function() {
	$("#creatpromodal").css('display', 'none');
	$("#programname").val('');
});
var routeContent = document.getElementById("route_content");
//确定新建项目
$("#submitbtn").on('click', function() {
	$("#creatpromodal").css('display', 'none');
	//将用户新建的文件名设为浏览器标题
	$("title").eq(0).html($("#programname").val());
	//创建option标签
	var $option = $('<option></option>');
	//将用户输入的文件名添加到option标签中
	$option.html($("#programname").val());
	//将option标签插入到下拉框中
	$("#operating_mode").append($option);
	//清空用户输入框的内容
	$("#programname").val('');

	//发送到控制器
	if(data_runstate == "stop")
	{
		if(workerObj_normal) {
			workerObj_normal.postMessage("newFile_:" + $("#programname").val() + ".txt");
		}
	}
	else
	{
		log("机器正在运行不可操作文件");
	}
	
	//刷新程序界面
	$("#blocklyDiv").html('');
	setTimeout(work, 10);
	function work() {
		workspace = Blockly.inject('blocklyDiv', {
			media: './media/',
			toolbox: document.getElementById('toolbox'),
			zoom: {
				controls: true,
				startScale: 1.2
			},
			trashcan: true
		});
		//获取代码
		workspace.addChangeListener(rightCodeEvent);
		function rightCodeEvent(masterEvent) {
			if(masterEvent.type == Blockly.Events.UI) {
				return;
			}
			code = Blockly.JavaScript.workspaceToCode(workspace) || '';
			$("#codeshow").val(code);
			console.log(code);
		}
	}
});

//获取代码
workspace.addChangeListener(rightCodeEvent);
function rightCodeEvent(masterEvent) {
	if(masterEvent.type == Blockly.Events.UI) {
		return;
	}
	code = Blockly.JavaScript.workspaceToCode(workspace) || '';
	$("#codeshow").val(code);
}
//获取xml文件
var dom;
var xml;
//保存代码按钮
$("#saveprogram").on('click', function() {
	dom = Blockly.Xml.workspaceToDom(workspace);
	xml = Blockly.Xml.domToText(dom);
	console.log(code);
	console.log(xml);

	//发送到控制器
	if(data_runstate == "stop")
	{
		if(workerObj_normal) {
			workerObj_normal.postMessage("saveFile:" + code);
		}
	}
	else
	{
		log("机器正在运行不可操作文件");
	}
	
	//将xml保存在本地
	//export_raw($("title").eq(0).html()+'.xml', xml);
});
//保存到本地
function fake_click(obj) {
	var ev = document.createEvent("MouseEvents");
	ev.initMouseEvent(
		"click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
	);
	obj.dispatchEvent(ev);
}
function export_raw(name, data) {
	var urlObject = window.URL || window.webkitURL || window;
	var oblob = new Blob([data]);
	var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
	save_link.href = urlObject.createObjectURL(oblob);
	save_link.download = name;
	fake_click(save_link);
}
//清空文件名
$("#deleteAllName").on('click', function() {
	$("#wenname").siblings().remove();
});

//删除单个文件名
//var optionname = document.getElementById("operating_mode");
//var alloptions = optionname.options;
//$("#deleteName").on('click', function() {
//	if(optionname.selectedIndex != 0) {
//		$("#operating_mode option").eq(optionname.selectedIndex).remove();
//	}
//});
//$("#operating_mode").on("change", function() {
//	if($("#operating_mode option").eq(optionname.selectedIndex).html() == "test") {
//		console.log(123);
//		$("title").eq(0).html($("#operating_mode option").eq(optionname.selectedIndex).html());
//		$.ajax({
//			//请求路径
//			url: 'js/test.txt',
//			//请求成功后回调的函数
//			success: function(result, status, xhr) {
//				console.log(result);
//				console.log(status);
//				console.log(xhr);
//
//				var oxml = Blockly.Xml.textToDom(result);
//				console.log(oxml);
//				Blockly.Xml.domToWorkspace(oxml, workspace);
//			}
//		});
//
//	};
//});